package top.chengdongqing.mishop.order

import org.springframework.http.HttpStatus
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.*
import top.chengdongqing.mishop.common.pageable.Page1
import top.chengdongqing.mishop.common.pageable.PageRequest1
import top.chengdongqing.mishop.entities.OrderEvent
import top.chengdongqing.mishop.enums.OrderStatus
import top.chengdongqing.mishop.order.vo.OrderVO

@RestController
@RequestMapping("/orders")
class OrderController(private val orderService: OrderService) {

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    fun createOrder(@AuthenticationPrincipal userId: Int, @RequestParam addressId: Int): Int {
        return orderService.createOrder(userId, addressId)
    }

    @GetMapping("/{id}")
    fun findOrder(@AuthenticationPrincipal userId: Int, @PathVariable id: Int): OrderVO {
        return orderService.findOrder(userId, id)
    }

    @GetMapping
    fun findAllByPage(
        @AuthenticationPrincipal userId: Int,
        keyword: String?,
        status: OrderStatus?,
        pageRequest: PageRequest1
    ): Page1<OrderVO> {
        return orderService.findAllByPage(userId, keyword, status, pageRequest.get())
    }

    @GetMapping("/{id}/events")
    fun findOrderEvents(@AuthenticationPrincipal userId: Int, @PathVariable id: Int): OrderEvent {
        return orderService.findOrderEvents(userId, id)
    }

    @PutMapping("/{id}/cancel")
    fun cancelOrder(@AuthenticationPrincipal userId: Int, @PathVariable id: Int) {
        orderService.cancelOrder(userId, id)
    }
}